package ch02sl.jd.jd2;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.net.MalformedURLException;
import java.util.Iterator;

public class Dom4JParse {
    public static void main(String[] args) {
        // 创建SAXReader对象
        SAXReader reader = new SAXReader();
        try {
            // 读取XML文件，生成Document对象
            Document document = reader.read(new File("src/test/java/ch02sl/jd/Student.XML"));
            // 获取根节点
            Element root = document.getRootElement();
            // 遍历Brand节点
            for (Iterator<Element> it = root.elementIterator("student"); it.hasNext(); ) {
                Element brand = it.next();
                // 获取Brand的属性值
                String brandName = brand.attributeValue("id");
                // 遍历Type节点
                for (Iterator<Element> typeIt = brand.elementIterator("Type"); typeIt.hasNext(); ) {
                    Element type = typeIt.next();
                    // 获取Type的属性值
                    String typeName = type.attributeValue("name");
                    String course = type.attributeValue("course");
                    String score = type.attributeValue("score");
                    // 输出品牌和型号
                    System.out.println("id" + brandName +"名字"+typeName+"课程"+course+"成绩"+score);
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }
}